// pages/music/music.js
Page({
    /**
     * 页面的初始数据
     */
    data: {
        src: '', // 背景图片
        isPlay: false, 
        isSingle: false, // 是否单曲循环
        videoSrc: '', // 初始音乐链接
        name: '念', // 初始音乐名
        songname:'薛之谦',
        value: 0, // 进度条当前值
        maxValue: 0, // 进度条最大值
        currentTime: '0:00', // 当前歌曲播放进度
        duration: '0:00', // 当前歌曲总时长
        index: 0, // 用于切换歌曲
        currentInd: 0, // 当前播放歌曲下标
        showAlert: false, // 列表弹窗的显隐
        videos: [
        {name:'精卫', singer: '30年前，50年后', videoSrc:'http://music.163.com/song/media/outer/url?id=1951069525.mp3'},
        {name:'可能', singer: '程响', videoSrc: 'http://music.163.com/song/media/outer/url?id=1950343972.mp3'},
        ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad (options) {
        console.log(options);
        this.setData({
            src:decodeURIComponent(options.url),
            videoSrc:decodeURIComponent(options.videoSrc),
            name:options.songname,
            songname:options.name,
            videos:JSON.parse(decodeURIComponent(options.list))
        })
        console.log(this.data.videos);
        this.audioCtx = wx.createInnerAudioContext()
        this.initialAudio()
      },
// 初始化音频
showActionSheet() {
    let list = this.data.videos.map(x => {return {"name":x.name,"singer": x.singer}})
    console.log(list);
    // let that = this
    this.setData({
      showAlert: true,
      list
    })
  },
initialAudio() {
    let audioCtx = this.audioCtx
    audioCtx.src = this.data.videoSrc // 初始歌曲
    audioCtx.loop = this.data.isSingle // 是否单曲循环
    // 当音乐自然播放完成后执行next()
    audioCtx.onEnded(() => {
        this.next()
    })
    // 获取音乐时长，当前进度
    audioCtx.onCanplay(() => {
    audioCtx.duration
    audioCtx.currentTime
    setTimeout(() => {
        this.setData({
            value: audioCtx.currentTime,
            maxValue: audioCtx.duration,
            currentTime: this.timeFormat(audioCtx.currentTime),
            duration: this.timeFormat(audioCtx.duration)
        })
    },1000)
    })
    // 音频播放进度更新后执行
    audioCtx.onTimeUpdate(() => {
        this.setData({
            value: audioCtx.currentTime,
            maxValue: audioCtx.duration,
            currentTime: this.timeFormat(audioCtx.currentTime),
            duration: this.timeFormat(audioCtx.duration)
        })
    })
  },
  timeFormat(e) {
    let time = Math.floor(e)
    const minutes = Math.floor(time / 60)
    const seconds = time % 60
    const result = `${this.padTo2Digits(minutes)}:${this.padTo2Digits(seconds)}`
    return result
},
padTo2Digits(num) {
    return num.toString().padStart(2, '0');
},
next() {
    let index = this.data.index + 1 // 下一首下标
    let list = this.data.videos  // 歌曲数组
    let isPlay = this.data.isPlay 
    let videoSrc = ''
    let name = ''
    console.log(list);
    console.log(index);
    console.log(list[index].videoSrc);
    // 如果超过列表长度，从列表第一首开始
    if(index>=list.length) {
        index = 0
    }
    videoSrc = list[index].videoSrc
    name = list[index].songname
    if(isPlay) {
        isPlay = !isPlay
    }
    this.setData({
        isPlay,
        index,
        currentInd: index,
        videoSrc: videoSrc,
        name: name
    })
    this.isPlay()
  },
  isPlay() {
    let isPlay = this.data.isPlay
    if(!isPlay) {
    this.audioCtx.src = this.data.videoSrc
    this.audioCtx.play()
    } else {
    this.audioCtx.pause()
    }
    this.setData({
        isPlay: !isPlay,
    })
  },
  pre() {
    let index = this.data.index - 1
    let list = this.data.videos
    let isPlay = this.data.isPlay
    let videoSrc = ''
    let name = ''
    if(index<0) {
        index = list.length - 1
    }
    videoSrc = list[index].videoSrc
    name = list[index].name
    if(isPlay) {
        isPlay = !isPlay
    }
    this.setData({
        isPlay,
        index,
        currentInd: index,
        videoSrc: videoSrc,
        name: name
    })
    this.isPlay()
  },
  isSingle() {
    let isSingle = this.data.isSingle
    this.setData({
        isSingle: !isSingle
    })
    this.audioCtx.loop = this.data.isSingle
  },
  toTime(e) {
    let time = e.detail.value
    this.audioCtx.seek(time)
  },
  onUnload() {
    // 页面卸载时暂停音乐播放
    this.audioCtx.pause();
  }
})